package club.wx3609.images.Utils;

import java.io.*;
import java.util.*;

/**
 * 配置文件工具类
 */
public class Configs {

    private static Properties properties=new Properties();

    private static String propertiesPath;

    private static FileOutputStream fileOutputStream;

    public static void setPropertiesPath(String propertiesPath){
        Configs.propertiesPath=propertiesPath;
        try {
            properties.load(Configs.class.getClassLoader().getResourceAsStream(propertiesPath));
            fileOutputStream=new FileOutputStream(Configs.class.getClassLoader().getResource(propertiesPath).getPath());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static String getPropertiesValue(String Key){
        if(IsNullUtils.character(Key)){
            return  properties.getProperty(Key);
        }
        return null;
    }

    public static void setProperties(String Key,String Value){
        try {
            Map toSaveMap = new HashMap<>();
            Set keys = properties.keySet();
            for (Iterator itr = keys.iterator(); itr.hasNext(); ) {
                String key = (String) itr.next();
                Object value = properties.get(key);
                toSaveMap.put(key, value);
            }
            toSaveMap.put(Key, Value);
            properties.putAll(toSaveMap);
            properties.store(fileOutputStream, "==== after add ====");
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
